package com.ssm.comm.eureka;

import com.netflix.appinfo.ApplicationInfoManager;
import com.netflix.appinfo.InstanceInfo;
import com.netflix.config.ConfigurationManager;
import com.netflix.discovery.DefaultEurekaClientConfig;
import com.netflix.discovery.DiscoveryManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.io.IOException;

@Configuration
public class EurekaRegister {

    /**
     * 默认的ribbon配置文件名, 该文件需要放在classpath目录下
     */
    public static final String RIBBON_CONFIG_FILE_NAME = "ribbon.properties";//就是配置负载均衡所需要的配置文件
    private static final Logger LOGGER = LoggerFactory.getLogger(EurekaRegister.class);
    public static boolean isRegister = false;


    @Bean
    public EurekaRegister  eurekaRegister2() { //辅助启动
        synchronized (EurekaRegister.class) {
            LOGGER.info("开始初始化ribbon");
            try {
                // 加载ribbon配置文件
                ConfigurationManager.loadPropertiesFromResources(RIBBON_CONFIG_FILE_NAME);
            } catch (IOException e) {
                e.printStackTrace();
                LOGGER.error("ribbon初始化失败");
                throw new IllegalStateException("ribbon初始化失败");
            }
            LOGGER.info("ribbon初始化完成");
            // 初始化Eureka Client
            LOGGER.info("Eureka初始化完成,正在注册Eureka Server");
            DiscoveryManager.getInstance().initComponent(new EurekaInstanceConfig(), new DefaultEurekaClientConfig());
            ApplicationInfoManager.getInstance().setInstanceStatus(InstanceInfo.InstanceStatus.UP);
        }

        return  new EurekaRegister();
    }



}
